
/* Include Files -------------------------------------------------------------------------------  */
#include "stm32f10x.h"
#include "driver.h"


/*  Macro Defines ------------------------------------------------------------------------------- */


/* Global Function Prototypes ------------------------------------------------------------------ */
int Drv_initDeviceHardware(void);
void watchdog_reset(void);

/* Private Function Prototypes ----------------------------------------------------------------- */
static void Drv_configDeviceClock(void);
static void Drv_configDevicePins(void);
static void Drv_configDeviceInterrupts(void);

/* Global Variables ---------------------------------------------------------------------------- */


/* Static Variables ---------------------------------------------------------------------------- */


 /**
  * @brief Drv_initDeviceHardware
  * @param  
  * @retval 
  * @note   
  */
int Drv_initDeviceHardware()
{
    Drv_configDeviceClock();
    Drv_configDevicePins();
		
		Dio_initModule();
    Sytm_initModule();
		Uart1_initModule();
    Uart2_initModule();
		Drv_configDeviceInterrupts();                   /* Configuring Processor Interrupt System */
    Siic_initModule();
		Moto_initModule();
    return 1;
}


/**
  * @brief Drv_configDeviceClock
  * @param  
  * @retval 
  * @note   
  */
static void Drv_configDeviceClock()
{
    /*RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_DMA2
                          | RCC_AHBPeriph_SRAM | RCC_AHBPeriph_FLITF
                          | RCC_AHBPeriph_CRC, ENABLE);*/

    RCC_APB1PeriphClockCmd(/*RCC_APB1Periph_PWR    | RCC_APB1Periph_CAN1 |*/ 
                           RCC_APB1Periph_USART2 /*| RCC_APB1Periph_USART3 | RCC_APB1Periph_UART4 | RCC_APB1Periph_UART5*/, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO | 
                           RCC_APB2Periph_GPIOA  | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC 
													/*| RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
                           | RCC_APB2Periph_SPI1
                           | RCC_APB2Periph_ADC1*/, ENABLE);

    PWR_BackupAccessCmd(ENABLE);
    RCC_LSEConfig(RCC_LSE_OFF);
    BKP_TamperPinCmd(DISABLE);
    PWR_BackupAccessCmd(DISABLE);
}

/**
  * @brief Drv_configDeviceClock
  * @param  
  * @retval 
  * @note   
  */
static void Drv_configDevicePins()
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* JTAG-DP Disabled and SW-DP Enabled */
    //GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
    //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);

    /* Configure I2C1 pins: PB6->SCL and PB7->SDA */
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	
		// lock check
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  							
		GPIO_Init(GPIOB, &GPIO_InitStructure);
	
		// Unlock check
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  				
		GPIO_Init(GPIOB, &GPIO_InitStructure);		
	
		// Motor check
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  							
		GPIO_Init(GPIOB, &GPIO_InitStructure);

    // Motor INA
		GPIO_InitStructure.GPIO_Pin =GPIO_Pin_11;	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
		
		// Motor INB
		GPIO_InitStructure.GPIO_Pin =GPIO_Pin_12;	
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);		
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                        /* UART1_TX */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	                    /* UART1_RX */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
	
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;                        /* UART2_TXD */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOA, &GPIO_InitStructure);

		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;	                       /* UART2_RXD */
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
		GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/**
  * @brief Drv_configDeviceInterrupts
  * @param  
  * @retval 
  * @note   
  */
static void Drv_configDeviceInterrupts(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  watchdog_reset.
  * @param  None
  * @retval None
  */
void watchdog_reset()
{
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
    IWDG_SetPrescaler(IWDG_Prescaler_32);
    IWDG_SetReload(50);
    IWDG_Enable();
//  while(1);
}

